<!DOCTYPE html>
<html lang="zh-cn">
<head>
  
    <link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=KOZafwuaDasEedEenI5aTy8aXH0epbm6VUJ0v3vsT_Q1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/ThinkInside/bundle-ThinkInside.css?v=RRjf6pEarGnbXZ86qxNycPfQivwSKWRa4heYLB15rVE1"/>
<link type="text/css" rel="stylesheet" href="/blog/customcss/428549.css?v=%2fam3bBTkW5NBWhBE%2fD0lcyJv5UM%3d"/>

</head>
<body>
<a name="top"></a>

<div id="page_begin_html"></div><script>load_page_begin_html();</script>

<div id="topics">
	<div class = "post">
		<h1 class = "postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/frankdeng/p/9017384.html">CentOS7.5安装Mysql5.7.22</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body" class="blogpost-body"><div class="content-list-text">
<h2>一 下载</h2>
<p>下载地址：<a href="https://www.mysql.com/downloads/" target="_blank">https://www.mysql.com/downloads/</a></p>
<p>1 点击第一个链接地址，进入MySQL官方网站，单击&ldquo;Downloads&rdquo;下载Tab页，进入下载界面</p>
</div>
<div class="content-list-media">
<div class="content-list-image clearfix"><img src="https://images2018.cnblogs.com/blog/1385722/201805/1385722-20180531205709075-1667725894.png" alt="" /></div>
</div>
<div class="content-list-text">
<p>2 找到Community并且单击它，出现以下的页面</p>
</div>
<div class="content-list-media">
<div class="content-list-image clearfix"><img src="https://images2018.cnblogs.com/blog/1385722/201805/1385722-20180531205816016-1281282810.png" alt="" /></div>
</div>
<div class="content-list-text">
<p>3 点击左侧第五个&ldquo;MySQL Community Server&rdquo;，社区版</p>
</div>
<div class="content-list-media">
<div class="content-list-image clearfix"><img src="https://images2018.cnblogs.com/blog/1385722/201805/1385722-20180531150911887-1897277969.png" alt="" /></div>
</div>
<div class="content-list-text">
<p>4 进入下拉下载列表，选择在CentOS7上tar包安装</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201805/1385722-20180531223419203-1244675718.png" alt="" /></p>
<p>4 进入下拉下载列表，这里选择在CentOS7上rpm包安装</p>
</div>
<div class="content-list-media">
<div class="content-list-image clearfix"><img src="https://images2018.cnblogs.com/blog/1385722/201805/1385722-20180531223803584-7809064.png" alt="" />
<p>5 点击Download，进入如下界面，需要登录下载，这里选择</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201805/1385722-20180531210404808-199390463.png" alt="" /></p>
<p>6 下载成功</p>
<h2>二&nbsp;RPM包安装</h2>
<p>&nbsp;下载文件名：mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar</p>
<h3>1 安装包准备</h3>
<p class="topic">1.1 查看mysql是否安装，如果安装了，卸载mysql</p>
<div class="cnblogs_code">
<pre>rpm -qa|grep mysql</pre>
</div>
<p class="topic">1.2 卸载</p>
<div class="cnblogs_code">
<pre>rpm -e --nodeps mysql-libs-<span style="color: #800080;">5.1</span>.<span style="color: #800080;">73</span>-<span style="color: #800080;">7</span>.el6.x86_64</pre>
</div>
<h3>2 解压安装</h3>
<p>2.1 创建目录</p>
<div class="cnblogs_code">
<pre>[root@node21 software]# mkdir -p /usr/local/mysql</pre>
</div>
<p>&nbsp;2.2 解压</p>
<div class="cnblogs_code">
<pre>[admin@node21 software]$ tar xvf /opt/software/mysql-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span>.el7.x86_64.rpm-<span style="color: #000000;">bundle.tar  -C /usr/local/mysql
mysql</span>-community-client-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm
mysql</span>-community-common-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm
mysql</span>-community-devel-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm
mysql</span>-community-embedded-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm
mysql</span>-community-embedded-compat-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm
mysql</span>-community-embedded-devel-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm
mysql</span>-community-libs-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm
mysql</span>-community-libs-compat-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm
mysql</span>-community-minimal-debuginfo-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm
mysql</span>-community-server-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm
mysql</span>-community-server-minimal-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm
mysql</span>-community-test-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span>.el7.x86_64.rpm</pre>
</div>
<p>2.3 安装</p>
<p>依次执行（几个包有依赖关系，所以执行有先后）下面命令安装</p>
<div class="cnblogs_code">
<pre>[root@node21 software]# cd /usr/local/mysql/<span style="color: #000000;">
[root@node21 mysql]# rpm </span>-ivh mysql-community-libs-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm
[root@node21 mysql]# rpm </span>-ivh mysql-community-client-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm
[root@node21 mysql]# rpm </span>-ivh mysql-community-server-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span>.el7.x86_64.rpm</pre>
</div>
<p>安装过程报错"</p>
<div class="cnblogs_code">
<pre>[root@node21 mysql]# rpm -ivh mysql-community-libs-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64.rpm 
warning: mysql</span>-community-libs-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span>.el7.x86_64.rpm: Header V3 DSA/<span style="color: #000000;">SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
    mysql</span>-community-common(x86-<span style="color: #800080;">64</span>) &gt;= <span style="color: #800080;">5.7</span>.<span style="color: #800080;">9</span> <span style="color: #0000ff;">is</span> needed by mysql-community-libs-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span><span style="color: #000000;">.el7.x86_64
    mariadb</span>-libs <span style="color: #0000ff;">is</span> obsoleted by mysql-community-libs-<span style="color: #800080;">5.7</span>.<span style="color: #800080;">22</span>-<span style="color: #800080;">1</span>.el7.x86_64</pre>
</div>
<p>&nbsp;警告：红帽安装rpm安装MySQL时爆出警告：mysql-community-libs-5.7.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature，密钥&nbsp;key ID 5072e1f5: NOKEY。</p>
<p>&nbsp;原因：这是由于yum安装了旧版本的GPG keys造成的</p>
<p>&nbsp;解决办法：后面加上 &nbsp;--force --nodeps 如：<span style="color: #ff0000;">rpm -ivh mysql-community-libs-5.7.22-1.el7.x86_64.rpm --force --nodeps&nbsp;&nbsp;</span>从 RPM 版本 4.1 开始，在安装或升级软件包时会检查软件包的签名。</p>
<h3>3 初始化数据库</h3>
<p>为了保证数据库目录为与文件的所有者为 mysql 登陆用户，如果你是以 root 身份运行 mysql 服务，需要执行下面的命令初始化<br />mysqld <span class="hljs-comment">--initialize --user=mysql</span></p>
<p>如果是以 mysql 身份运行，则可以去掉&nbsp;<code>--user</code>&nbsp;选项。</p>
<p>另外&nbsp;<code>--initialize</code>&nbsp;选项默认以&ldquo;安全&rdquo;模式来初始化，则会为 root 用户生成一个密码并将该密码标记为过期，登陆后你需要设置一个新的密码，而使用&nbsp;<code>--initialize-insecure</code>&nbsp;命令则不使用安全模式，则不会为 root 用户生成一个密码。</p>
<p>这里演示使用的&nbsp;<code>--initialize</code>&nbsp;初始化，会生成一个 root 账户密码，密码在log文件里，红色区域的就是自动生成的密码</p>
<div class="cnblogs_code">
<pre>[root@node21 mysql]# mysqld --initialize --user=<span style="color: #000000;">mysql<br /></span></pre>
</div>
<p>mysql配置文件在/etc/my.cnf,里面指明了数据目录。日志位置</p>
<div class="cnblogs_code">
<p>[root@node21 mysql]# cat /var/log/mysqld.log<br />2018-05-19T05:53:03.738829Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentat<br />ion for more details).2018-05-19T05:53:03.739304Z 0 [ERROR] Can't find error-message file '/usr/share/mysql/errmsg.sys'. Check error-message file location and 'lc-messages-dir' configuratio<br />n directive.2018-05-19T05:53:08.434726Z 0 [Warning] InnoDB: New log files created, LSN=45790<br />2018-05-19T05:53:08.872448Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.<br />2018-05-19T05:53:08.954300Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new U<br />UID: 0fb455fb-6560-11e8-a2f0-000c29c94111.2018-05-19T05:53:08.956569Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.<br />2018-05-19T05:53:08.958761Z 1 [Note] A temporary password is generated for root@localhost:<span style="color: #ff0000;"> ?2nt4!-t&amp;s#M</span></p>































</div>
<h3>4 启动数据库</h3>
<div class="cnblogs_code">
<pre>[root@node21 software]# mysql -uroot -<span style="color: #000000;">p<br />Enter password:<br /></span></pre>
</div>
<h3>5 修改密码</h3>
该密码被标记为过期了，如果想正常使用还需要修改密码&nbsp;
<p class="hljs cpp"><code class="lang-shell hljs">mysql&gt; show databases; </code></p>
<p class="hljs cpp"><code class="lang-shell hljs">ERROR <span class="hljs-number">1820 (HY000): You must reset your password <span class="hljs-keyword">using ALTER USER statement before executing <span class="hljs-keyword">this statement. </span></span></span></code></p>
<p>以前的&nbsp;<code>password()</code>函数将会被抛弃，官方建议使用下面的命令来修改密码&nbsp;</p>
<p class="hljs lua"><code class="lang-shell hljs">mysql&gt; ALTER USER <span class="hljs-string">'root'@<span class="hljs-string">'localhost' IDENTIFIED BY <span class="hljs-string">'new_password'; </span></span></span></code></p>
<p>同时，如果你设置的密码过于简单也会报错。&nbsp;</p>
<p class="hljs lua"><code class="lang-shell hljs">mysql&gt; ALTER USER <span class="hljs-string">'root'@<span class="hljs-string">'localhost' IDENTIFIED BY <span class="hljs-string">'123';</span></span></span></code></p>
<p class="hljs lua"><code class="lang-shell hljs"><span class="hljs-string"><span class="hljs-string"><span class="hljs-string">ERROR <span class="hljs-number">1819 (HY000): Your password does <span class="hljs-keyword">not satisfy the current policy requirements</span></span></span></span></span></code></p>
<h3>6 配置user表</h3>
<p>&nbsp;配置主要是root用户+密码，在任何主机上都能登录MySQL数据库。</p>
<p>6.1 查看表结构</p>
<div class="cnblogs_code">
<pre>mysql&gt;desc user;</pre>
</div>
<p>6.2 查询user表</p>
<div class="cnblogs_code">
<pre>mysql&gt;<span style="color: #0000ff;">select</span> User,Host,Password <span style="color: #0000ff;">from</span><span style="color: #000000;"> user;    #旧版本
mysql</span>&gt;<span style="color: #0000ff;">select</span> User,host,authentication_string <span style="color: #0000ff;">from</span> user; #<span style="color: #800080;">5</span>.7版本以后</pre>
</div>
<p>6.3&nbsp;修改user表，把Host表内容修改为%</p>
<div class="cnblogs_code">
<pre>mysql&gt; update user <span style="color: #0000ff;">set</span> host=<span style="color: #800000;">'</span><span style="color: #800000;">%</span><span style="color: #800000;">'</span> <span style="color: #0000ff;">where</span> host=<span style="color: #800000;">'</span><span style="color: #800000;">localhost</span><span style="color: #800000;">'</span>;</pre>
</div>
<p>6.4&nbsp;删除root用户的其他host</p>
<div class="cnblogs_code">
<pre>mysql&gt; delete <span style="color: #0000ff;">from</span> user <span style="color: #0000ff;">where</span> host != <span style="color: #800000;">'</span><span style="color: #800000;">%</span><span style="color: #800000;">'</span>;</pre>
</div>
<p>最后结果显示</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201806/1385722-20180601154915406-1791743117.png" alt="" /></p>
<p>6.5 刷新退出</p>
<div class="cnblogs_code">
<pre>mysql&gt;<span style="color: #000000;"> flush privileges;
Query OK, </span><span style="color: #800080;">0</span> rows affected (<span style="color: #800080;">0.01</span><span style="color: #000000;"> sec)
mysql</span>&gt;<span style="color: #000000;"> exit;
Bye</span></pre>
</div>
<h3>7 设置开机启动</h3>
<div class="cnblogs_code">
<pre><span style="color: #000000;">[root@node21 mysql]# systemctl enable mysqld
[root@node21 mysql]# systemctl list</span>-unit-files |<span style="color: #000000;"> grep mysqld
mysqld.service                                enabled </span></pre>
</div>
<h2>三 mysql故障解析</h2>
<h3>1 忘记用户密码</h3>
首先确认服务器出于安全的状态，也就是没有人能够任意地连接MySQL数据库。 因为在重新设置MySQL的root密码的期间，MySQL数据库完全出于没有密码保护的状态下，其他的用户也可以任意地登录和修改MySQL的信息。可以采用将MySQL对&nbsp;外的端口封闭，并且停止Apache以及所有的用户进程的方法实现服务器的准安全状态。最安全的状态是到服务器的Console上面操作，并且拔掉网线。&nbsp;<br />
<p>1.1 vi /etc/my.cnf</p>
<p>1.2 在[mysqld]中添加&nbsp; &nbsp;skip-grant-tables 保存退出</p>
<p>
1.3 重启mysql&nbsp; &nbsp;&nbsp;systemctl restart mysqld</p>
<p>
1.4 .用户无密码登录</p>
<p>
mysql -uroot -p (直接点击回车，密码为空)</p>
<p>
1.5 选择数据库</p>
<p><strong>
mysql&gt;&nbsp;use mysql;</strong></p>
<p>
1.6 修改root密码</p>
<p>MySQL&nbsp;5.7 开始&nbsp;数据库中的user 表中已经将<em><strong>password</strong></em>&nbsp;字段更改为<strong>authentication_string&nbsp;</strong>&nbsp;所以更改密码 直接使用&nbsp;</p>
<p><strong>mysql&gt;&nbsp;</strong><strong>update&nbsp;user set authentication_string=password('123456') where user='root';</strong></p>
<p class="hljs lua"><strong><code class="lang-shell hljs">mysql&gt; ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';<br /></code></strong></p>
<p>1.7 执行</p>
<p><strong>mysql&gt; flush privileges ;</strong></p>
<p>1.8 退出</p>
<p><strong>mysql&gt;&nbsp;&nbsp;exit;</strong></p>
<p>9.删除权限检查</p>
<p>将刚才在[mysqld]的段中加上的skip-grant-tables删除</p>
<p>10.重启mysql</p>
<p>&nbsp;systemctl restart mysqld</p>
<h3>2.ssl_cipher报错</h3>
<p>错误描述：</p>
<p>添加用户 insert into mysql.user(Host,User,Password) values("localhost","test",password("1234"));</p>
<p>报以下的错误 ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value错误</p>
<p>mysql5.1以上版本，我是在5.6版本上操作的。</p>
<p>错语原因：</p>
<p>mysql用户表的中某些字段不能为空，没有默认值，其实是操作错误，mysql添加用户是不能这样直接insert user表的。</p>
<p>解决方法：</p>
<p>正确的添加用户方法：</p>
<p>GRANT USAGE ON *.* TO 'user01'@'localhost' IDENTIFIED BY '123456' WITH GRANT OPTION;</p>
<p>用户：user01，密码：123456，这样就添加了一个新的用户，不会出以上的错误了。</p>
<h3>3.添加用户报错</h3>
<p>安装<a class="keylink" href="https://www.2cto.com/database/MySQL/" target="_blank">mysql</a>的时候出现这个问题，在增加用户的时候</p>
<p>The MySQL server is running with the &ndash;skip-grant-tables option so it cannot execute this statement</p>
<p>出现lush privileges 一下就OK了，</p>
<p>mysql&gt; flush privileges;</p>
<p>Query OK, 0 rows affected (0.01 sec)</p>
<p>&ndash;skip-grant-tables</p>
<p>顾名思义，<a class="keylink" href="https://www.2cto.com/database/" target="_blank">数据库</a>启动的时候 跳跃权限表的限制，不用验证密码，直接登录。</p>
<h2>四&nbsp;mysql-connector-java与mysql版本的对应关系</h2>
<p>官网文档地址：</p>
<p><a title="version5.1" href="https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-versions.html" target="_blank">https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-versions.html</a>&nbsp;（Connector/J Versions 5.1）</p>
<p><a title="version6.0" href="https://dev.mysql.com/doc/connector-j/6.0/en/connector-j-versions.html" target="_blank">https://dev.mysql.com/doc/connector-j/8.0/en/connector-j-versions.html</a>&nbsp;（Connector/J Versions 8.0）</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201807/1385722-20180729144149835-1871508330.png" alt="" /></p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201807/1385722-20180729144102994-2071979095.png" alt="" /></p>
<p class="title">mysql-connector-java下载地址 ：<a href="http://central.maven.org/maven2/mysql/mysql-connector-java/" target="_blank">http://central.maven.org/maven2/mysql/mysql-connector-java/</a></p>






























</div>






























</div></div>

</body>
</html>
